package com.season.order.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 陈正果
 * @date 2020/6/30 1:19
 */

/**
 * rabbitMq配置类
 */
@Configuration
public class RabbitMqConfig {
    /**
     * 订单创建交换机名
     */
    public static final String EX_ORDER_CREATE = "ex_order_create";
    /**
     * 订单创建队列名
     */
    public static final String QUEUE_ORDER_CREATE = "queue_order_create";
    /**
     * 订单支付成功交换机名
     */
    public static final String EX_ORDER_SUCCESS = "ex_order_success";
    /**
     * 订单支付成功队列名
     */
    public static final String QUEUE_ORDER_SUCCESS = "queue_order_success";
    /**
     * 订单支付成功更新会员队列名
     */
    public static final String QUEUE_ORDER_SUCCESS_MEMBER = "queue_order_success_member";

    /**
     * 定义订单创建队列
     *
     * @return 队列
     */
    @Bean(name = QUEUE_ORDER_CREATE)
    public Queue queue_order_create() {
        return new Queue(QUEUE_ORDER_CREATE);
    }

    /**
     * 定义订单支付成功队列
     *
     * @return 队列
     */
    @Bean(name = QUEUE_ORDER_SUCCESS)
    public Queue queue_order_success() {
        return new Queue(QUEUE_ORDER_SUCCESS);
    }

    /**
     * 定义订单支付成功更新会员队列
     *
     * @return
     */
    @Bean(name = QUEUE_ORDER_SUCCESS_MEMBER)
    public Queue queue_order_success_member() {
        return new Queue(QUEUE_ORDER_SUCCESS_MEMBER);
    }

    /**
     * 定义订单创建交换机
     *
     * @return 交换机
     */
    @Bean(name = EX_ORDER_CREATE)
    public Exchange ex_order_create() {
        return ExchangeBuilder.fanoutExchange(EX_ORDER_CREATE).durable(true).build();
    }

    /**
     * 定义订单支付成功交换机
     *
     * @return 交换机
     */
    @Bean(name = EX_ORDER_SUCCESS)
    public Exchange ex_order_success() {
        return ExchangeBuilder.fanoutExchange(EX_ORDER_SUCCESS).durable(true).build();
    }

    /**
     * 订单创建队列绑定交换机
     *
     * @param exchange 订单创建交换机
     * @param queue    订单创建队列
     * @return Binding
     */
    @Bean(name = "BINDING_QUEUE_ORDER_CREATE")
    public Binding binding_queue_order_create(@Qualifier(EX_ORDER_CREATE) Exchange exchange,
                                              @Qualifier(QUEUE_ORDER_CREATE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    /**
     * 订单支付成功队列绑定交换机
     *
     * @param exchange 订单支付成功交换机
     * @param queue    订单支付成功队列
     * @return Binding
     */
    @Bean(name = "BINDING_QUEUE_ORDER_SUCCESS")
    public Binding binding_queue_order_success(@Qualifier(EX_ORDER_SUCCESS) Exchange exchange,
                                               @Qualifier(QUEUE_ORDER_SUCCESS) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    /**
     * 订单支付成功更新会员队列绑定交换机
     *
     * @param exchange 订单支付成功交换机
     * @param queue    订单支付成功更新会员队列
     * @return Binding
     */
    @Bean
    public Binding BINDING_QUEUE_ORDER_SUCCESS_MEMBER(@Qualifier(EX_ORDER_SUCCESS) Exchange exchange,
                                                      @Qualifier(QUEUE_ORDER_SUCCESS_MEMBER) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
